package managedbean.FRM.CreditAccount;

import entity.CRM.Customer;
import entity.CRM.SaleLead;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.CRM.SaleLeadBeanLocal;
import session.stateless.FRM.CreditAccountBeanLocal;

@ManagedBean(name="addDepositManagerBean")
@ViewScoped
public class AddDepositManagerBean implements Serializable {
    public AddDepositManagerBean() {}
    
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    @EJB
    private SaleLeadBeanLocal saleLeadBeanLocal;
    
    private Customer selectedCustomer;
    private Double depositAdd;
    private String statusMessage;
    private boolean isExisting;
    private String saleLeadName;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedCustomer!=null)
                this.setIsExisting(true);
            else this.setIsExisting(false);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }

    public boolean isIsExisting() {
        return isExisting;
    }

    public void setIsExisting(boolean isExisting) {
        if (selectedCustomer!= null)
            this.isExisting=true;
        else this.isExisting=false;
        
    }

    public String getSaleLeadName() {
        return saleLeadName;
    }

    public void setSaleLeadName(String saleLeadName) {
        this.saleLeadName = saleLeadName;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Double getDepositAdd() {
        return depositAdd;
    }

    public void setDepositAdd(Double depositAdd) {
        this.depositAdd = depositAdd;
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }
    
    public void saveNewDeposit(ActionEvent event)
    {
        creditAccountBeanLocal.addDeposit(selectedCustomer.getRegistrationNumber(), depositAdd);
        statusMessage = "New deposit of $" +this.depositAdd+ " has been added Successfully. Your credit limit has been updated to " +customerBeanLocal.findCustomerById(selectedCustomer.getRegistrationNumber()).getCreditAccount().getCreditLimit(); 
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage + "." , "")); 
        
        this.setSelectedCustomer(customerBeanLocal.findCustomerById(selectedCustomer.getRegistrationNumber()));
    }
    
    public void retrieve()
    {
        if (saleLeadName!=null)
        {
            this.setSelectedCustomer(customerBeanLocal.findCustomerByName(saleLeadName));
            System.out.println("retrieve done");
        }
    }
    
    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        List<SaleLead> list = saleLeadBeanLocal.getAllSalesLead();
        List<Customer> list2 = customerBeanLocal.getAllCustomers();
        
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getSaleLeadName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getSaleLeadName());
        }
        
        for (int i = 0; i < list2.size(); ++i)
        {
            if (list2.get(i).getSaleLeadName().toLowerCase().contains(query.toLowerCase()))
                results.add(list2.get(i).getSaleLeadName());
        }
        return results;  
    }
        
}
